package com.sojson.controller.po;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.TagsParamVerify;
import com.sojson.entity.po.Tags;
import com.sojson.result.Result;
import com.sojson.service.TagsService;

import io.swagger.annotations.Api;

/**
 * 商品标签表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "商品标签表管理")
@RestController
@RequestMapping(value = "/tags")
public class TagsController {

    @Resource
    private TagsService tagsServiceImpl;

    /**
     * 单条插入
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    @PostMapping(value = "/insert")
    @PreAuthorize("@ss.hasPermi('/tags/insert')")
    public Object insert(@RequestBody Tags tags) throws SQLException {
        // 校验参数
        Result<?> paramResult = TagsParamVerify.insert(tags);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        tags.insertInit();
        tagsServiceImpl.insert(tags);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Tags tags) throws SQLException {
        return Result.success(tagsServiceImpl.insertList(tags));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Tags tags) throws SQLException {
        return Result.success(tagsServiceImpl.deleteByDto(tags));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = tagsServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Tags> findAll = tagsServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Tags tags) throws SQLException {
        int countByDto = tagsServiceImpl.countByDto(tags);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/tags/findByDto')")
    public Object findByDto(Tags tags) throws SQLException {
        List<Tags> findByDto = tagsServiceImpl.findByDto(tags);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Object deleteById(Integer id) throws SQLException {
        tagsServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @DeleteMapping(value = "/deleteByIdList")
    @PreAuthorize("@ss.hasPermi('/tags/deleteByIdList')")
    public Object deleteByIdList(@RequestParam("ids") ArrayList<Integer> ids) throws SQLException {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(tagsServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Tags tags) throws SQLException {
        tagsServiceImpl.updateById(tags);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Tags tags) throws SQLException {
        return Result.success(tagsServiceImpl.updateStaticByIdList(tags));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Tags tags) throws SQLException {
        return Result.success(tagsServiceImpl.updateChangeByIdList(tags));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/tags/updatePartialById')")
    public Object updatePartialById(@RequestBody Tags tags) throws SQLException {
        // 校验参数
        Result<?> paramResult = TagsParamVerify.updatePartialById(tags);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        tags.updateInit();
        tagsServiceImpl.updatePartialById(tags);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Tags tags) throws SQLException {
        return Result.success(tagsServiceImpl.updateStaticPartialByIdList(tags));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Tags tags) throws SQLException {
        return Result.success(tagsServiceImpl.updateChangePartialByIdList(tags));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) throws SQLException {
        Tags findById = tagsServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Tags> findByIdList = tagsServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Tags tags) throws SQLException {
        ArrayList<Integer> findIdByDto = tagsServiceImpl.findIdByDto(tags);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        tagsServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param tags
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findAppByDto")
    public Object findAppByDto(Tags tags) throws SQLException {
        List<Tags> findByDto = tagsServiceImpl.findByDto(tags);
        return Result.success(findByDto, findByDto);
    }

}